package cn.wolfcode.Controller;


import cn.wolfcode.domain.Question;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;


    @RequirePermission(name = "问题表页面",experssion = "question:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Question> pageInfo = questionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "问题表页面",experssion = "question:list")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            questionService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "问题表页面",experssion = "question:list")
    @ResponseBody
    public jsonResult saveOrUpdate(Question question){
        if (question.getId() != null) {
            questionService.update(question);
        }else {
            questionService.save(question);
        }
        return new jsonResult();
    }
}
